package cn.fl.framework.base.context;

import java.util.HashMap;
import java.util.List;

import cmm.web.core.framework.constant.SysOrgConstant;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.framework.base.constant.WebBaseConstant;

/**
 * @description:线程变量
 * @param <T>
 */
public class CurrentThreadContext {

	private static ThreadLocal<HashMap<String, Object>> threadLocals = new ThreadLocal<HashMap<String, Object>>();
	
	public static final String CURRENT_USER_ID = "currentUserId";//用户ID
	public static final String CURRENT_USER_NAME = "currentUserName";//用户名称
	public static final String CURRENT_USER_INFO = "currentUserInfo";//用户信息
	public static final String CURRENT_SYS_CD = "currentSysCd";//当前系统代码
	public static final String CURRENT_ORG_CD = "currentOrgCd";//当前组织机构代码
	public static final String CURRENT_ORG_ID = "currentOrgId";//当前组织机构ID
	public static final String CURRENT_ROLE_ID = "currentRoleId";//当前人员角色
	public static final String CURRENT_ROLE_GRP_ID = "currentRoleGrpId";//当前人员角色组
	public static final String CURRENT_ORG_NM = "currentOrgNm";//当前组织机构名称
	public static final String CURRENT_ORG_SHT_NM = "currentOrgShtNm";//当前组织机构简称
	public static final String CURRENT_ORG_DISP_NM = "currentOrgDispNm";//当前组织机构显示名称
	public static final String CURRENT_ROLE_NM = "currentRoleNm";//当前人员角色名称
	public static final String CURRENT_CHANGE_ROLE_FLAG = "currentLoginChangeRoleFlag";//当前人员角色名称
	public static final String CURRENT_ORG_TYP_CD = "currentOrgTypCd";//当前组织机构类型代码
	public static final String CURRENT_ORG_RGN_CD = "currentOrgRgnCd";//当前机构的区域代码
	public static final String CURRENT_ORG_RGN_CD_NM = "currentOrgRgnCdNm";//当前机构的区域代码
	public static final String CURRENT_RES_POWER_NUM="currentResPowerNum";//本地存储版本信息
	public static final String CURRENT_RES_POWER="currentResPower";//本地存储版本信息
	public static final String CURRENT_RES_POWER_URL="currentResPowerUrls";//权限地址集合

	/**
	 * 取得当前用户id
	 */
	public static Long getCurrentUserId() {
		Object object = getValue(CURRENT_USER_ID);
		if (object != null) {
			return (Long) object;
		}
		return null;//10000L;
	}

	/**
	 * 取得当前用户姓名
	 */
	public static String getCurrentUserName() {
		Object object = getValue(CURRENT_USER_NAME);
		if (object != null) {
			return (String) object;
		}
		return null;//"管理员10000";
	}
	
	/**
	 * 取得当前组织架构代码
	 */
	public static String getCurrentOrgCd() {
		Object object = getValue(CURRENT_ORG_CD);
		if (object != null) {
			return (String) object;
		}
		return null;//BaseConstant.SYS_ORG_CD;
	}
	
	/**
	 * 取得当前组织架构所属业务部代码
	 */
	public static String getCurrentBuOrgCd() {
		String orgCd = getCurrentOrgCd();
		if (orgCd != null) {
			//这里要根据指定业务部门长度进行截取业务部代码,要注意非业务部门情况
			int length = SysOrgConstant.SYS_ORG_CD_LENGTH * SysOrgConstant.SYS_BU_ORG_CD_LVL;
			if(orgCd.length() > length){//截取
				orgCd = orgCd.substring(0, length);
				BaseLogUtils.println("截取组织架构事业部内容："+orgCd);
			}
			return orgCd;
		}
		return null;//BaseConstant.SYS_ORG_CD;
	}
	
	/**
	 * 取得当前组织机构简称
	 */
	public static String getCurrentOrgShtNm() {
		Object object = getValue(CURRENT_ORG_SHT_NM);
		if (object != null) {
			return (String) object;
		}
		return null;
	}
	
	/**
	 * 取得当前组织机构显示简称
	 */
	public static String getCurrentOrgDispNm() {
		Object object = getValue(CURRENT_ORG_DISP_NM);
		if (object != null) {
			return (String) object;
		}
		return null;
	}
	
	/**
	 * 取得当前组织架构代码
	 */
	public static Long getCurrentOrgId() {
		Object object = getValue(CURRENT_ORG_ID);
		if (object != null) {
			return (Long) object;
		}
		return null;//BaseConstant.SYS_ORG_CD;
	}
	
	/**
	 * 取得当前系统代码
	 */
	public static String getCurrentSysCd() {
		Object object = getValue(CURRENT_SYS_CD);
		if (object != null) {
			return (String) object;
		}
		return WebBaseConstant.SYS_SYS_CD;
	}
	
	/**
	 * 获取重构服务系统代码
	 */
	public static String getFlServiceSysCd() {
		return PropertiesCacheUtils.getProperty(WebBaseConstant.FL_SERVICE_SYS_CD);
	}
	
	/**
	 * 获取重构支付APP渠道代码
	 */
	public static String getFlPayAppChannelCd() {
		return PropertiesCacheUtils.getProperty(WebBaseConstant.FL_PAY_APP_CHANNEL_CD);
	}
	
	/**
	 * 取得当前角色ID
	 */
	public static Long getCurrentRoleId() {
		Object object = getValue(CURRENT_ROLE_ID);
		if (object != null) {
			return (Long) object;
		}
		return null;//BaseConstant.CURRENT_ROLE_ID;
	}
	
	/**
	 * 取得当前系统代码
	 */
	@SuppressWarnings("unchecked")
	public static List<Long> getCurrentRoleGrpId() {
		Object object = getValue(CURRENT_ROLE_GRP_ID);
		if (object != null) {
			return  (List<Long>)object;
		}
		return null;//BaseConstant.CURRENT_ROLE_GRP_ID;
	}

	/**
	 * @MethodName getCurrentRgnDivCd
	 * @Description: 获取当前用户所属区域代码
	 * @Author: wangpengtao@lionbridgecapital.cn
	 * @CreateDate: 2019/1/23 20:57
	 * @Version: 1.0.0
	 * @Param
	 * @Return java.lang.String
	 * @UpdateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	public static String getCurrentRgnDivCd(){
		Object object = getValue(CURRENT_ORG_RGN_CD);
		if (object != null) {
			return  (String)object;
		}
		return null;//BaseConstant.CURRENT_ROLE_GRP_ID;
	}

	/**
	 * @MethodName getCurrentRgnDivCdNm
	 * @Description: 获取当前用户所属区域名称
	 * @Author: wangpengtao@lionbridgecapital.cn
	 * @CreateDate: 2019/1/23 20:57
	 * @Version: 1.0.0
	 * @Param
	 * @Return java.lang.String
	 * @UpdateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	public static String getCurrentRgnDivCdNm(){
		Object object = getValue(CURRENT_ORG_RGN_CD_NM);
		if (object != null) {
			return  (String)object;
		}
		return null;//BaseConstant.CURRENT_ROLE_GRP_ID;
	}

	/**
	 * @MethodName getCurrentRgnDivCdNm
	 * @Description: 获取当前用户角色可以访问地址
	 * @Author: wangpengtao@lionbridgecapital.cn
	 * @CreateDate: 2019/1/23 20:57
	 * @Version: 1.0.0
	 * @Param
	 * @Return java.lang.String
	 * @UpdateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	public static List<String> getCurrentResPowerUrl(){
		Object object = getValue(CURRENT_RES_POWER_URL);
		if (object != null) {
			return  (List<String>)object;
		}
		return null;//BaseConstant.CURRENT_ROLE_GRP_ID;
	}
	/**
	 * 取得线程变量值
	 * 
	 * @param name
	 *            线程变量名
	 * @return 线程变量值
	 */
	public static Object getValue(String name) {
		if (threadLocals.get() == null) {
			threadLocals.set(new HashMap<String, Object>());
		}
		return threadLocals.get().get(name);
	}

	/**
	 * 设置线程变量值
	 * 
	 * @param name
	 *            线程变量名
	 * @param value
	 *            线程变量值
	 */
	public static void setValue(String name, Object value) {
		if (threadLocals.get() == null) {
			threadLocals.set(new HashMap<String, Object>());
		}
		threadLocals.get().put(name, value);
	}

	public static void clear() {
		if (threadLocals.get() != null) {
			threadLocals.get().clear();
		}
	}
}
